package com.esun.server.config;

import com.esun.server.exceptionHandler.AsyncExceptionHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

/**
 * 异步线程池设置
 *
 * @author xiaoliebin
 */
@Configuration
@EnableAsync
@EnableScheduling
public class AsyncConfig implements AsyncConfigurer {
	private static Logger logger = LoggerFactory.getLogger(AsyncConfig.class);

	private int corePoolSize = 10;

	private int maxPoolSize = 10;

	private int queueCapacity = 4000;

	private boolean waitForTasksToCompleteOnShutdown = true;

	private int awaitTerminationSeconds = 60 * 10;

	private String threadNamePrefix = "AsyncThread-";


	@Override
	@Bean("taskExecutor")
	public Executor getAsyncExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		//设置核心线程数
		executor.setCorePoolSize(corePoolSize);
		//设置最大线程数
		executor.setMaxPoolSize(maxPoolSize);
		//设置队列容量
		executor.setQueueCapacity(queueCapacity);
		//设置是否等待
		executor.setWaitForTasksToCompleteOnShutdown(waitForTasksToCompleteOnShutdown);
		//设置等待时间
		executor.setAwaitTerminationSeconds(awaitTerminationSeconds);
		//设置线程前缀
		executor.setThreadNamePrefix(threadNamePrefix);
		executor.initialize(); //如果不初始化，导致找到不到执行器
		return executor;
	}

	@Override
	public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
		return new AsyncExceptionHandler();
	}


	public void setCorePoolSize(int corePoolSize) {
		this.corePoolSize = corePoolSize;
	}

	public void setMaxPoolSize(int maxPoolSize) {
		this.maxPoolSize = maxPoolSize;
	}

	public void setQueueCapacity(int queueCapacity) {
		this.queueCapacity = queueCapacity;
	}

	public void setWaitForTasksToCompleteOnShutdown(boolean waitForTasksToCompleteOnShutdown) {
		this.waitForTasksToCompleteOnShutdown = waitForTasksToCompleteOnShutdown;
	}

	public void setAwaitTerminationSeconds(int awaitTerminationSeconds) {
		this.awaitTerminationSeconds = awaitTerminationSeconds;
	}

	public void setThreadNamePrefix(String threadNamePrefix) {
		this.threadNamePrefix = threadNamePrefix;
	}
}
